package org.chs.ts.batch.exception;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.Getter;
import org.chs.ts.base.batch.exec.entity.BatchDailyBasicTaskStepExec;
import org.chs.ts.base.batch.log.entity.BatchClearBasicLog;
import org.chs.ts.base.batch.set.entity.BatchDailyBasicTaskPublish;
import org.chs.ts.base.common.util.SpringContextUtils;
import org.chs.ts.base.constant.CommonConstant;
import org.chs.ts.batch.constant.TaskDailyStatusCodeEnum;
import org.chs.ts.batch.factory.SernoFactory;
import org.chs.ts.batch.service.IBatchClearBasicLogService;
import org.chs.ts.batch.service.IBatchDailyBasicTaskStepExecService;
import org.chs.ts.batch.threadservice.BatchDailyServiceBase;
import org.chs.ts.batch.util.ConvertToUtil;



import java.util.HashMap;

/**
 * @Compay 盛唐天行
 * @Author: wuwei
 * @Censor By wuwei
 * @Sence 2024/6/13 15:07
 * @Modify 2024/6/13 15:07
 * @Description 日间跑批异常对象
 */
public class TaskDailyException extends Exception {

    // private static Logger log = LoggerFactory.getLogger(TaskStepExecException.class);

    private static final String SERNO_TYPE = "taskDailyStep";

    @Getter
    private final String code;

    @Getter
    private final String taskExecid;

    private IBatchDailyBasicTaskStepExecService dailyTaskStepExecService;

    protected IBatchClearBasicLogService clearBasicLogService;


    public TaskDailyException(String str, TaskDailyStatusCodeEnum taskDailyStepStatusCodeEnum,
                              BatchDailyServiceBase<BatchDailyBasicTaskPublish, BatchDailyBasicTaskStepExec> batchServiceBase) {

        super(str);

        this.initSpring();

        this.code = taskDailyStepStatusCodeEnum.getRetCd();

        this.taskExecid = batchServiceBase.getTaskDailyStepObject().getTaskExecid();

        createClearBasicLog(batchServiceBase);

        // 初始化 spring bean
        // log.info(this.getMessage());
    }

    public TaskDailyException(String str, TaskDailyStatusCodeEnum taskDailyStatusCodeEnum, String taskExecid) {
        super(str);
        this.code = taskDailyStatusCodeEnum.getRetCd();
        this.taskExecid = taskExecid;

        // 初始化 common spring 对象
        initSpring();
        createClearBasicLog(taskExecid);
    }

    protected void createClearBasicLog(String taskExecid) {
        String endTime = ConvertToUtil.getRunningTime();
        String execStatus = "4";

        dailyTaskStepExecService.updateByTaskExecid(taskExecid, execStatus);
        createClearBasicLog(0, "ip", "port", "path",
                dailyTaskStepExecService.getOne(new LambdaQueryWrapper<BatchDailyBasicTaskStepExec>().eq(BatchDailyBasicTaskStepExec::getTaskExecid
                        , taskExecid)), execStatus, endTime);

    }

    protected void createClearBasicLog(long startTime, String taskExecid, String ip, String port, String path,
                                       BatchDailyBasicTaskStepExec taskDailyStepObject) {
        String endTime = ConvertToUtil.getRunningTime();
        String execStatus = "4";

        dailyTaskStepExecService.updateTaskDailyStepByObj(taskExecid, ip + ":" + port, path,
                ConvertToUtil.timeMillisFormatDateTime(startTime),
                ConvertToUtil.timeMillisFormatDateTime(Long.valueOf(endTime)), CommonConstant.RTN_CODE_FAILED,
                execStatus);

        createClearBasicLog(startTime, ip, port, path, taskDailyStepObject, execStatus, endTime);
    }

    private void createClearBasicLog(long startTime, String ip, String port, String path, BatchDailyBasicTaskStepExec taskDailyStepObject,
                                     String execStatus, String endTime) {
        String rtnDesc = getLogMessage(getMessage());
        clearBasicLogService.save(
                ConvertToUtil.convertToV(new BatchClearBasicLog(), taskDailyStepObject, new HashMap<String, Object>() {
                    {
                        put("logSerno", SernoFactory.createSerno(SERNO_TYPE));
                        put("stepNo", taskDailyStepObject.getStepNo());
                        put("execStatus", execStatus);
                        put("shouldExecDate", taskDailyStepObject.getExecTime());
                        put("taskExecid", taskDailyStepObject.getTaskExecid());
                        put("targetCode", "");
                        put("startTime", ConvertToUtil.timeMillisFormatDateTime(startTime));
                        put("endTime", ConvertToUtil.timeMillisFormatDateTime(Long.valueOf(endTime)));
                        put("threadUuid", "");
                        put("serverName", path);
                        put("serverIp", ip + ":" + port);
                        put("rtnCode", CommonConstant.RTN_CODE_FAILED);
                        put("rtnDesc", rtnDesc);
                    }
                })


        );
    }

    protected void createClearBasicLog(BatchDailyServiceBase<BatchDailyBasicTaskPublish, BatchDailyBasicTaskStepExec> batchServiceBase) {


        if (batchServiceBase == null || batchServiceBase.getTaskDailyStepObject() == null) {
            return;
        }

        BatchDailyBasicTaskStepExec taskDailyStepObject = batchServiceBase.getTaskDailyStepObject();
        String ip = batchServiceBase.getIp();
        String port = batchServiceBase.getPort();
        String path = batchServiceBase.getPath();
        long startTime = batchServiceBase.getTaskDailyStepStartTime();

        createClearBasicLog(startTime, taskExecid, ip, port, path, taskDailyStepObject);

    }

    protected String getLogMessage(String msg) {
        int strLength = 1800;
        // return msg.length() > strLength ? msg.substring(0, strLength) : msg;
        return msg.substring(0, Math.min(strLength, msg.length()));
    }

    protected void initSpring() {

        this.dailyTaskStepExecService = (IBatchDailyBasicTaskStepExecService) SpringContextUtils.getBean(
                "batchDailyBasicTaskStepExecServiceImpl");
    }


}
